package com.xiaoq.sites.endpoint;


import java.util.ArrayList;
import java.util.List;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.feature.entity.CommentEntity;
import com.xiaoq.feature.entity.ShopFoodEntity;
import com.xiaoq.feature.entity.ShopMenusEntity;
import com.xiaoq.sites.entity.SitesEntity;
import com.xiaoq.sites.entity.SitesPointEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags={"解决七牛云图片访问问题"})
@RestController
@RequestMapping("/api/v1/solution")
public class QiniuSolutionEndpoint {
	
	@Autowired
	private Datastore datastore;
	
	@ApiOperation(value = "替换七牛云景区图片", notes = "替换七牛云景区图片")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericDataResponse.class, message = "替换七牛云景区图片") })
	@PutMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public void updateSitesImage(){
		Query<SitesEntity> sitesQuery = datastore.createQuery(SitesEntity.class);
		List<SitesEntity> sites = sitesQuery.asList();
		for(SitesEntity site:sites){
			
			String mainImgUrl = site.getMainImgUrl();
			if(!StringUtils.isEmpty(mainImgUrl)){
				mainImgUrl = myReplace(mainImgUrl);
				site.setMainImgUrl(mainImgUrl);
			}
			String logo = site.getLogo();
			if(!StringUtils.isEmpty(logo)){
				logo = myReplace(logo);
				site.setLogo(logo);
			}
			
			List<String> imgUrls = site.getImgUrls();
			if(!CollectionUtils.isEmpty(imgUrls)){
				List<String> imgUrlss = new ArrayList<>();
				for(String imgUrl:imgUrls){
					imgUrl = myReplace(imgUrl);
					imgUrlss.add(imgUrl);
				}
				site.setImgUrls(imgUrlss);
				
			}
			
			String miniImgUrl = site.getMiniImgUrl();
			
			if(!StringUtils.isEmpty(miniImgUrl)){
				miniImgUrl = myReplace(miniImgUrl);
				site.setMiniImgUrl(miniImgUrl);
				
			}
			datastore.save(site);
		}
		

	}
	
	@ApiOperation(value = "替换七牛云景点图片", notes = "替换七牛云景点图片")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericDataResponse.class, message = "替换七牛云景点图片") })
	@PutMapping(value = "/sitesPoint", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public void updateSitesPointImage(){
		Query<SitesPointEntity> sitesPointQuery = datastore.createQuery(SitesPointEntity.class);
		List<SitesPointEntity> sitePoints = sitesPointQuery.asList();
		for(SitesPointEntity sitesPoint:sitePoints){
			String mainImgUrl = sitesPoint.getMainImgUrl();
			if(!StringUtils.isEmpty(mainImgUrl)){
				mainImgUrl = myReplace(mainImgUrl);
				sitesPoint.setMainImgUrl(mainImgUrl);
				
			}
			List<String> imgUrls = sitesPoint.getImgUrls();
			if(!CollectionUtils.isEmpty(imgUrls)){
				List<String> imgUrlss = new ArrayList<>();
				for(String imgUrl:imgUrls){
					imgUrl = myReplace(imgUrl);
					imgUrlss.add(imgUrl);
				}
				sitesPoint.setImgUrls(imgUrlss);
			}
			datastore.save(sitesPoint);
			
			
		}
		
	}
	
	@ApiOperation(value = "替换七牛云评论图片", notes = "替换七牛云评论图片")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericDataResponse.class, message = "替换七牛云评论图片") })
	@PutMapping(value = "/comment", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public void updateCommentImage(){
		Query<CommentEntity> commentQuery = datastore.createQuery(CommentEntity.class);
		List<CommentEntity> comments = commentQuery.asList();
		for(CommentEntity comment:comments){
			String userLogo = comment.getUserLogo();
			if(!StringUtils.isEmpty(userLogo)){
				userLogo = myReplace(userLogo);
				comment.setUserLogo(userLogo);
				
			}
			
			List<String> attachImgUrls = comment.getAttachImgUrls();
			if(!CollectionUtils.isEmpty(attachImgUrls)){
				List<String> attachImgUrlss = new ArrayList<>();
				for(String attachImgUrl:attachImgUrls){
					attachImgUrl = myReplace(attachImgUrl);
					attachImgUrlss.add(attachImgUrl);
				}
				
				comment.setAttachImgUrls(attachImgUrlss);
				
			}
			datastore.save(comment);
			
		}
		
	}
	
	@ApiOperation(value = "替换七牛云必吃图片", notes = "替换七牛云必吃图片")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericDataResponse.class, message = "替换七牛云必吃图片") })
	@PutMapping(value = "/shopFood", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public void updateShopFoodImage(){
		Query<ShopFoodEntity> shopFoodQuery = datastore.createQuery(ShopFoodEntity.class);
		List<ShopFoodEntity> shopFoods = shopFoodQuery.asList();
		for(ShopFoodEntity shopFood:shopFoods){
			String mainImgUrl = shopFood.getMainImgUrl();
			if(!StringUtils.isEmpty(mainImgUrl)){
				mainImgUrl = myReplace(mainImgUrl);
				shopFood.setMainImgUrl(mainImgUrl);
				
			}
			
			List<ShopMenusEntity> cuisinesList = shopFood.getCuisinesList();
			if(!CollectionUtils.isEmpty(cuisinesList)){
				for(ShopMenusEntity shopMenus:cuisinesList){
					List<String> imgUrls = shopMenus.getImgUrls();
					if(!CollectionUtils.isEmpty(imgUrls)){
						List<String> imgUrlss = new ArrayList<>();
						for(String imgUrl:imgUrls){
							imgUrl = myReplace(imgUrl);
							imgUrlss.add(imgUrl);
						}
						shopMenus.setImgUrls(imgUrlss);
					}
					
				}
				
			}
			
			datastore.save(shopFood);
			
			
		}
		
	}
	
	public String myReplace(String secense){
		
			return secense.replace("p3cif3r0q.bkt.clouddn.com", "qiniurepository.xiaoq.online");
		
	}

}
